<?php
/**
* Functions to Output Header Slider
* 
*/


/**
 * webtreats_fading_header_slider() - outputs HTML for fading header slider
 * 
 */
function webtreats_fading_header_slider(){
	global $shortname, $homepage_slider_source;
	
	if($homepage_slider_source == 'custom'){
		$get_options = get_option($shortname.'_slider_settings');
	}
	
	if($homepage_slider_source == 'categories'){
		$get_options = webtreats_category_slider();
		$number = $get_options['counter']+1; $numbers = range(1,$number);
	}
	
	if(get_option($shortname.'_slider_settings') == false) { $get_options = webtreats_preview_slider();  }

	foreach ($get_options as $key => $value ) {
		if ( preg_match("/(fading_slider_)/i", $key) ) {
			$$key = $value;	
		}
	}

	if($homepage_slider_source == 'custom'){
		$number = $fading_slider_count; $numbers = range(1,$number);
	}
	
	$out .= '<div id="webtreats_fading_slider">';
	$out .= '<div id="loading_slider"></div>';
	$out .= '<div class="fading_slides noscript">';
	
	$counter = 0;
	foreach ($numbers as $number) {
		
	$slider_img = 'fading_slider_url_' . $counter;
	$slider_link = 'fading_slider_link_' . $counter;
	$slider_title = 'fading_slider_title_' . $counter;
	$slider_stage = 'fading_slider_stage_' . $counter;
	$slider_desc = 'fading_slider_desc_' . $counter;
	$slider_btn = 'fading_slider_btn_' . $counter;
	$disable_text = 'fading_slider_text_disable_' . $counter;
	
	if (($$slider_stage == "floating_slide") || ($$slider_stage == "staged_slide")){$img_height=350; $img_width=900;}
	if ($$slider_stage == "partial_staged_slide"){$img_height=334; $img_width=567;}
	if ($$slider_stage == "partial_gradient_slide"){ $img_height=344; $img_width=510;}
	if ($$slider_stage == "full_slide"){$img_height=420; $img_width=980;}
	
	$img_resize = webtreats_image_resize($img_height, $img_width, $$slider_img);
	

	$out .= '<div class="single_fading_slide ' .$$slider_stage. '">';
	
	if($$slider_img){ 
	
		if(preg_match_all('!.+\.(?:jpe?g|png|gif)!Ui',$$slider_img,$matches)){
			if($$slider_link){
				$out .= '<div class="positioning"><a class="load_slider_img" href="' .$$slider_link. '"><span class="rm_slider_img"><img alt="" class="single_slide_image" src="' .$img_resize. '" /></span></a></div>'; 
			}else{
				$out .= '<div class="positioning load_slider_img"><span class="rm_slider_img"><img alt="" class="single_slide_image" src="' .$img_resize. '" /></span></div>';
			}
		}

		
		if(preg_match_all('#http://(www.vimeo|vimeo)\.com(/|/clip:)(\d+)(.*?)#i',$$slider_img,$matches)){
			
			$img_width +=1;
			
			$slider_video = '[vimeo framed="0" height="' .$img_height. '" width="' .$img_width. '"]' .$$slider_img. '[/vimeo]';
			$out .= '<div class="positioning" style="width:' .$img_width. 'px;">' .do_shortcode($slider_video). '</div>';
		}
		
		
		if(preg_match( '#http://(www.youtube|youtube|[A-Za-z]{2}.youtube)\.com/(.*?)#i', $$slider_img, $matches )){
			
			$img_width +=1;
			
			$slider_video = '[youtube framed="0" height="' .$img_height. '" width="' .$img_width. '"]' .$$slider_img. '[/youtube]';
			$out .= '<div class="positioning" style="width:' .$img_width. 'px;">' .do_shortcode($slider_video). '</div>';
		}
		
		
	}
		
			
	if( (!$$disable_text && $$slider_desc) || ($$slider_title) ){ 
		$out .= '<div class="slider_content">';
		if($$slider_title){ 
			$slider_title = stripslashes($$slider_title);
			$out .= '<h2 class="slider_title">' .do_shortcode($slider_title). '</h2>'; 
			}
			
		if($$slider_desc){ 
			$slider_desc = stripslashes($$slider_desc);
			$out .=  '<p>' .do_shortcode($slider_desc). '</p>'; 
			}
			
		if($$slider_link && !$$slider_btn){ $out .= '<p><a class="button_link" href="' .$$slider_link. '"><span>Read More</span></a></p>'; }
		$out .= '</div>';
	}
		
	$out .= '</div>';

	$counter++;
	}
	
	$out .= '</div><!-- fading_slides -->';
	
	$one_slide = ($number == 1) ? ' noscript' : '';
	
	$out .= '<div class="slidetabs' .$one_slide. '">';
	
	foreach ($numbers as $number) { $out .= '<a href="#"></a>'; $counter++; }
	
	$out .= '</div><!-- slidetabs -->
	</div><!-- webtreats_fading_slider -->';
	
	echo $out;
}


/**
 * webtreats_tabbed_header_slider() - outputs HTML for tabbed header slider
 * 
 */
function webtreats_tabbed_header_slider(){
	global $shortname, $homepage_slider_source;
	
	if($homepage_slider_source == 'custom'){
		$get_options = get_option($shortname.'_slider_settings');
	}
	
	if($homepage_slider_source == 'categories'){
		$get_options = webtreats_category_slider();
		$number = $get_options['counter']+1; $numbers = range(1,$number);
	}
	
	foreach ($get_options as $key => $value ) {
		if ( preg_match("/(tabbed_slider_)/i", $key) ) {
			$$key = $value;	
		}
	}
	
	if($homepage_slider_source == 'custom'){
		$number = $tabbed_slider_count; $numbers = range(1,$number);
	}
	
	$out .= '<div id="loading_slider" style="position:absolute;"></div>';
	$out .= '<div id="webtreats_tabbed_slider">';
	$out .= '<div id="tabbed_slides">';

	
	$counter = 0;
	foreach ($numbers as $number) {
		
		$slider_img = 'tabbed_slider_url_' . $counter;
		$slider_link = 'tabbed_slider_link_' . $counter;
		$slider_title = 'tabbed_slider_title_' . $counter;
		$slider_stage = 'tabbed_slider_stage_' . $counter;
		$slider_desc = 'tabbed_slider_desc_' . $counter;
		$slider_btn = 'tabbed_slider_btn_' . $counter;
		$disable_text = 'tabbed_slider_text_disable_' . $counter;

		if (($$slider_stage == "floating_slide") || ($$slider_stage == "staged_slide")){$img_height=350; $img_width=900;}
		if ($$slider_stage == "partial_staged_slide"){$img_height=334; $img_width=567;}
		if ($$slider_stage == "partial_gradient_slide"){ $img_height=344; $img_width=510;}
		if ($$slider_stage == "full_slide"){$img_height=420; $img_width=980;}
		
		$img_resize = webtreats_image_resize($img_height, $img_width, $$slider_img);

		
		$out .= '<div class="single_tabbed_slide ' . $$slider_stage . ' noscript" style="z-index:1000;">';
		
		if($$slider_img){ 

			if(preg_match_all('!.+\.(?:jpe?g|png|gif)!Ui',$$slider_img,$matches)){
				if($$slider_link){
					$out .= '<div class="positioning"><a class="load_slider_img" href="' .$$slider_link. '"><span class="rm_slider_img"><img alt="" class="single_slide_image" src="' .$img_resize. '" /></span></a></div>'; 
				}else{
					$out .= '<div class="positioning load_slider_img"><span class="rm_slider_img"><img alt="" class="single_slide_image" src="' .$img_resize. '" /></span></div>';
				}
			}


			if(preg_match_all('#http://(www.vimeo|vimeo)\.com(/|/clip:)(\d+)(.*?)#i',$$slider_img,$matches)){

				$img_width +=1;

				$slider_video = '[vimeo framed="0" height="' .$img_height. '" width="' .$img_width. '"]' .$$slider_img. '[/vimeo]';
				$out .= '<div class="positioning" style="width:' .$img_width. 'px;">' .do_shortcode($slider_video). '</div>';
			}


			if(preg_match( '#http://(www.youtube|youtube|[A-Za-z]{2}.youtube)\.com/(.*?)#i', $$slider_img, $matches )){

				$img_width +=1;

				$slider_video = '[youtube framed="0" height="' .$img_height. '" width="' .$img_width. '"]' .$$slider_img. '[/youtube]';
				$out .= '<div class="positioning" style="width:' .$img_width. 'px;">' .do_shortcode($slider_video). '</div>';
			}


		}

				
		if( (!$$disable_text && $$slider_desc) || ($$slider_title) ){ 
			$out .= '<div class="slider_content">';
			if($$slider_title){ 
				$slider_title = stripslashes($$slider_title);
				$out .= '<h2 class="slider_title">' .do_shortcode($slider_title). '</h2>';
				}
				
			if($$slider_desc){
				$slider_desc = stripslashes($$slider_desc);
				$out .=  '<p>' .do_shortcode($slider_desc). '</p>';
				}
				
			if($$slider_link && !$$slider_btn){ $out .= '<p><a class="button_link" href="' .$$slider_link. '"><span>Read More</span></a></p>'; }
			$out .= '</div>';
		}
				
				
		$out .= '</div>';



	$counter++;
	}
	
	$out .= '</div><!-- tabbed_slides -->';
	
	$out .= '<div class="clearboth"></div><!-- required for IE6/IE7 -->';
	
	$out .= '<div id="thumbs" class="t noscript">';
	
	$out .= '<div class="navi">';
	
	$counter = 0;
	foreach ($numbers as $number) {
		
		$slider_thumb = 'tabbed_slider_url_' . $counter;
		
		if(preg_match_all('!.+\.(?:jpe?g|png|gif)!Ui',$$slider_thumb,$matches)){
			$slider_thumb = $$slider_thumb;
		}else{
			$slider_thumb = get_template_directory_uri() .'/styles/_shared/default_movie_thumb_small.png';
		}
		
		$active = ($counter == 0) ? ' active' : '';
						
		$out .= '<a class="tabbed_slider_thumb' .$active. '"><img alt="" class="fade_hover" src="' .webtreats_image_resize($img_height=45, $img_width=64, $slider_thumb). '" /></a>';
		
		$counter++;
		
	}
		
	$out .= '</div><!-- navi -->';	
		
	$out .= '</div><!-- thumbs -->';
	
	$out .= '</div><!-- webtreats_tabbed_slider -->';
	
	echo $out;
	
}


/**
 * webtreats_category_slider() - retrieves post meta for category slider
 * 
 */
function webtreats_category_slider(){
	global $post, $wpdb, $homepage_slider, $homepage_slider_source, $slider_showcats, $slider_count;
	$slider_settings = array();
	$counter = 0;
	
	$cat_slider_query = new WP_Query("cat=$slider_showcats&showposts=$slider_count");
	
	if($cat_slider_query->have_posts()) : while($cat_slider_query->have_posts()) : $cat_slider_query->the_post();
	
		$custome_image = get_post_meta($post->ID, 'frontpage_image', true);
		$stage_effect = get_post_meta($post->ID, 'frontpage_stage', true);
		$disable_text = get_post_meta($post->ID, 'frontpage_disable_text', true);
	
		$slider_settings[$homepage_slider.'_slider_url_'.$counter] = $custome_image;
		$slider_settings[$homepage_slider.'_slider_link_'.$counter] = get_permalink($post->ID);
		$slider_settings[$homepage_slider.'_slider_title_'.$counter] = the_title( $before, $after, $display=false);
		$slider_settings[$homepage_slider.'_slider_desc_'.$counter] = webtreats_excerpt(200, ' ... ');
		$slider_settings[$homepage_slider.'_slider_stage_'.$counter] = $stage_effect;
		$slider_settings[$homepage_slider.'_slider_text_disable_'.$counter] = $disable_text;
		
		$slider_settings['counter'] = $counter;
	
		$counter++;
	
	endwhile; endif;
	
	wp_reset_query(); 

	return $slider_settings;
}

/**
 * webtreats_preview_slider() - slider images for WordPress theme preview on 1st time activation
 * 
 */
function webtreats_preview_slider(){
	
	$options_slider = array(
	
		"fading_slider_count" => 5,
	
		"fading_slider_url_0" => WEBTREATS_IMAGES . "/sliders/default_floating_slide.jpg",
		"fading_slider_link_0" => "",
		"fading_slider_title_0" => "",
		"fading_slider_stage_0" => "staged_slide",
		"fading_slider_desc_0" => "",
	
		"fading_slider_url_1" => WEBTREATS_IMAGES . "/sliders/default_partial_staged_slide.jpg",
		"fading_slider_link_1" => "http://memamw.me",
		"fading_slider_title_1" => "Partial Staged Efffect",
		"fading_slider_stage_1" => "partial_staged_slide",
		"fading_slider_desc_1" => "To easily create this slide effect simply choose 'Partial Staged' in the homepage slider settings and your image will be cropped and framed for you. ",
	
		"fading_slider_url_2" => WEBTREATS_IMAGES . "/sliders/default_partial_gradient_slide.jpg",
		"fading_slider_link_2" => "http://memamw.me",
		"fading_slider_title_2" => "Partial Gradient Efffect",
		"fading_slider_stage_2" => "partial_gradient_slide",
		"fading_slider_desc_2" => "To easily create this slide effect simply choose 'Partial Gradient' in the homepage slider settings and your image will be cropped and framed for you. ",
	
		"fading_slider_url_3" => WEBTREATS_IMAGES . "/sliders/default_floating_slide.jpg",
		"fading_slider_link_3" => "",
		"fading_slider_title_3" => "",
		"fading_slider_stage_3" => "floating_slide",
		"fading_slider_desc_3" => "",
	
		"fading_slider_url_4" => WEBTREATS_IMAGES . "/sliders/default_full_slide.jpg",
		"fading_slider_link_4" => "",
		"fading_slider_title_4" => "",
		"fading_slider_stage_4" => "full_slide",
		"fading_slider_desc_4" => "",
	
		);
		
	return $options_slider;
}

/**
 * webtreats_header_slider_switcher() - switches header slider 
 * 
 * @param string $homepage_slider name of slider selected
 */
function webtreats_header_slider_switcher($homepage_slider){

	if($homepage_slider == 'fading'){
		webtreats_fading_header_slider();
	}

	if($homepage_slider == 'tabbed'){
		webtreats_tabbed_header_slider();
	}

}


?>